package com.wn.icloud.common.utils;

import com.alibaba.fastjson.JSON;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 树节点工具
 *
 * @param <T>
 */
public class NodeUtil<T extends TreeNode> implements Serializable {

    // 子节点集合
    private List<T> returnList = new ArrayList<>();

    // 横向遍历后的有序集合
    private List<T> horizList = new ArrayList<>();

    /**
     * 获取横向遍历后有序的节点集合
     *
     * @param list
     * @param typeId 父ID
     * @return
     */
    public List<T> getHorizNodes(List<T> list, String typeId) {
        getChildNodes(list, typeId);
        return this.horizList;
    }

    /**
     * 获取压平的节点
     *
     * @param list
     * @param typeId
     * @return
     */
    public Map<String, T> getFlattenNodes(List<T> list, String typeId) {
        Map<String, T> map = new HashMap<>();
        List<T> sches = this.getChildNodes(list, typeId);
        for (T sche : sches) {
            map.put(sche.getId(), sche);
        }
        return map;
    }

    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list   分类表
     * @param typeId 传入的父节点ID（父节点ID有对应的实体）
     * @return String
     */
    public List<T> getChildNodes(List<T> list, String typeId) {
        if (list == null && typeId == null)
            return null;
        for (T t : list) {
            if (typeId.equals(t.getId())) {
                horizList.add(t); // 把根节点放入
                recursionFn(list, t);
            }
        }
        return returnList;
    }

    // 递归查找子节点
    private void recursionFn(List<T> list, T TreeNode) {
        returnList.add(TreeNode); // 先把当前节点加入
        List<T> childList = getChildList(list, TreeNode);// 得到子节点列表
        if (childList.size() > 0) {// 判断是否有子节点
            for (T n : childList) {
                recursionFn(list, n);
            }
        }
    }

    // 得到子节点列表
    private List<T> getChildList(List<T> list, T TreeNode) {
        List<T> nodeList = new ArrayList<>();
        for (T n : list) {
            if (n.getParentId().equals(TreeNode.getId())) {
                nodeList.add(n);
            }
        }
        // 可在此处排序
        nodeList.sort((T node1, T node2) -> node2.getEndTime().compareTo(node1.getEndTime())); // 按结束时间倒排
        this.horizList.addAll(nodeList);
        return nodeList;
    }

    /**
     * 纵向遍历一颗树
     *
     * @param t
     * @return
     */
    public List<T> getVertical(T t) {
        List<T> list = new ArrayList<>();
        list.add(t);
        list.addAll(traverse(t));
        return list;
    }

    /**
     * 遍历一颗树，层次遍历
     *
     * @param t
     * @return
     */
    private List<T> traverse(T t) {
        List<T> totList = new ArrayList<>();
        if (t != null) {
            List<T> tList = t.getChildren();
            for (int i = 0; i < tList.size(); i++) {
                T temp = tList.get(i);
                totList.add(temp);
                if (temp.getChildren().size() > 0) {
                    totList.addAll(traverse(temp));
                }
            }
        }
        return totList;
    }

	public static void main(String[] args) {
		List<DeptRelation> deptRelations = new ArrayList<>();
		DeptRelation d1 = new DeptRelation(1, 0);
        DeptRelation d7 = new DeptRelation(7, 3);
		DeptRelation d2 = new DeptRelation(2, 1);
		DeptRelation d3 = new DeptRelation(3, 1);
		DeptRelation d4 = new DeptRelation(4, 2);
		DeptRelation d5 = new DeptRelation(5, 2);
		DeptRelation d6 = new DeptRelation(6, 3);
		deptRelations.add(d7);
		deptRelations.add(d6);
		deptRelations.add(d5);
		deptRelations.add(d4);
		deptRelations.add(d3);
		deptRelations.add(d2);
		deptRelations.add(d1);
		NodeUtil<DeptRelation> nodeUtil2 = new NodeUtil<>();
		List<DeptRelation> deptRelations2 = nodeUtil2.getHorizNodes(deptRelations, "1");
		System.err.println(JSON.toJSONString(deptRelations2));
	}

}




























